package com.cms.stu.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cms.stu.entity.ClassEntity;
import com.cms.stu.to.StudentDetailTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cms.stu.entity.StudentEntity;
import com.cms.stu.service.StudentService;
import com.cms.common.utils.PageUtils;
import com.cms.common.utils.R;



/**
 * 
 *
 * @author weixiaochun
 * @email xiaochun0121@163.com
 * @date 2022-03-15 10:53:38
 */
@RestController
@RequestMapping("stu/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/login")
    public R login(@RequestBody StudentEntity studentEntity){
        StudentEntity student = studentService.login(studentEntity);
        if(student != null){
            return R.ok().put("student", student);
        }else{
            return R.error();
        }
    }

    /**
     * 根据学生IDs查询所在的所有班级
     */
    @RequestMapping("/selectClassInfoByStudentIds")
    //@RequiresPermissions("stu:student:list")
    public R selectClassInfoByStudentIds(@RequestParam(value = "studentIds", required = false) List<Long> studentIds){
        List<ClassEntity> classEntities = studentService.selectClassInfoByStudentIds(studentIds);

        return R.ok().put("classEntities", classEntities);
    }

    /**
     * 根据班级ID查询学生ID
     */
    @RequestMapping("/selectStudentInfoByClassId")
    //@RequiresPermissions("stu:student:list")
    public R selectStudentInfoByClassId(@RequestParam(value = "classId", required = false) Long classId, @RequestParam(value = "studentName", required = false) String studentName){
        List<StudentDetailTo> studentDetailTos = studentService.selectStudentInfoByClassId(classId, studentName);

        return R.ok().put("studentDetailTos", studentDetailTos);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("stu:student:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = studentService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listNoPage")
    //@RequiresPermissions("stu:student:list")
    public R listNoPage(){
        List<StudentEntity> list = studentService.list();

        return R.ok().put("students", list);
    }

    /**
     * 根据学生ID获取班级信息
     */
    @RequestMapping("/selectInfoById/{id}")
    //@RequiresPermissions("stu:student:info")
    public R selectInfoById(@PathVariable("id") Long id){
        StudentEntity student = studentService.selectInfoById(id);

        return R.ok().put("student", student);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("stu:student:info")
    public R info(@PathVariable("id") Long id){
		StudentEntity student = studentService.getById(id);

        return R.ok().put("student", student);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoByClassId/{classId}")
    //@RequiresPermissions("stu:student:info")
    public R infoByClassId(@PathVariable("classId") Long classId){
        List<StudentEntity> students = studentService.list(new QueryWrapper<StudentEntity>().eq("class_id", classId));

        return R.ok().put("students", students);
    }

    /**
     * 信息
     */
    @RequestMapping("/infoByNum/{num}")
    //@RequiresPermissions("stu:student:info")
    public R infoByNum(@PathVariable("num") String num){
        StudentEntity student = studentService.getOne(new QueryWrapper<StudentEntity>().eq("num", num));

        return R.ok().put("student", student);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("stu:student:save")
    public R save(@RequestBody StudentEntity student){
		studentService.autoSave(student);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("stu:student:update")
    public R update(@RequestBody StudentEntity student){
		studentService.updateById(student);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("stu:student:delete")
    public R delete(@RequestBody Long[] ids){
		studentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
